﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using STX.Admin.Models.BizMessage;
using STX.Domain;
using STX.Service;

namespace STX.Admin.Controllers
{
    public class BizMessageController : BasePages1Controller<BizMessage, IndexModel, AddModel, EditModel, QueryModel>
    {
        public BizMessageService BizMessageService { get; set; }



        protected override void OnInit(out Func<BaseService<BizMessage>> thisService, out Func<IndexModel> getIndexModelFunc, out Func<AddModel> getAddModelFunc, out Func<int, EditModel> getEditModelFunc)
        {
            thisService = () => BizMessageService;
            getIndexModelFunc = () => new IndexModel();
            getAddModelFunc = () => new AddModel();
            getEditModelFunc = id =>
            {
                var model = BizMessageService.FindModelById(id);
                if (model == null)
                {
                    throw new Exception("此数据可能已被删除");
                }
                return new EditModel
                {
                    Id = id,
                    Content = model.Content,
                    Position = model.Position,
                    Title = model.Title
                };
            };
        }

        protected override long AddHandler(AddModel addModel)
        {
            var model = new BizMessage
            {
                Title = addModel.Title,
                Content = addModel.Content,
                AddTime = DateTime.Now,
                Position = addModel.Position
            };
            return BizMessageService.Add(model);
        }

        protected override bool EditHandler(EditModel editModel)
        {
            var model = BizMessageService.FindModelById(editModel.Id);
            if (model == null)
            {
                throw new Exception("此数据可能已被删除");
            }
            model.Title = editModel.Title;
            model.Content = editModel.Content;
            model.Position = editModel.Position;
            return BizMessageService.Update(model);
        }

        

        protected override IQueryable<BizMessage> QueryHandler(QueryModel queryModel, IQueryable<BizMessage> queryable)
        {
            if (!queryModel.Name.IsNullOrEmpty())
                queryable = queryable.Where(o => o.Title.Contains(queryModel.Name));
            return queryable;
        }

        public ActionResult Detail(int id)
        {
            var model = BizMessageService.FindModelById(id);
            if (model == null)
            {
                throw new Exception("此数据可能已被删除");
            }
            var dto = new EditModel
            {
                Id = id,
                Content = model.Content,
                Position = model.Position,
                Title = model.Title,
                AddTime = model.AddTime
            };
            return View(dto);
        }
    }
}
